<?php

namespace App\Models;

use App\Models\customer\NodeModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class TakeOrderModel extends BaseModel
{
    use HasFactory;

    protected $table = 't_take_order';

    protected $primaryKey = 'id';

    public $timestamps = true;

    const CREATED_AT = 'created_at';
    
    const UPDATED_AT = null;

    protected $guarded = [];

    protected $hidden = [
        'deleted_at',
    ];

    const CARGO_STATUS_NO = 0;
    const CARGO_STATUS_YES = 1;
    const CARGO_STATUS_MAPS = [
        self::CARGO_STATUS_NO => '未确认',
        self::CARGO_STATUS_YES => '已确认',
    ];

    //1配舱完成 2抵达 3清关 4签收
    const STATUS_CREATE = 1;
    const STATUS_ARRIVE = 2;
    const STATUS_CLEARANCE = 3;
    const STATUS_SIGN = 4;
    const STATUS_TAKEOFF = 5;
    const STATUS_MAPS = [
        self::STATUS_CREATE => '配舱完成',
        self::STATUS_ARRIVE => '已抵达',
        self::STATUS_CLEARANCE => '已清关',
        self::STATUS_SIGN => '已派送',
        self::STATUS_TAKEOFF => '已起飞',
    ];

    //客户展示提单号 1不可见 2可见
    const CUSTOMER_SHOW = 2;
    const CUSTOMER_HIDE = 1;
    const SHOW_MAPS = [
        self::CUSTOMER_SHOW => '展示',
        self::CUSTOMER_HIDE => '不展示',
    ];

    //1收1/4  2收1/2  3收1/3 4收2/3 5收全泡
    public static $rec_p = [
        1 => '收1/4',
        2 => '收1/2',
        3 => '收1/3',
        4 => '收2/3',
        5 => '收全泡',
        6 => '不收泡',
    ];

    //报关类型 1=9610快件报关 2买单报关 3单证报关
    const DEC_TYPE_MAPS = [
        1 => '9610快件报关',
        2 => '买单报关',
        3 => '单证报关',
    ];

    //报关方式 1我司入仓 2客户自理
    const DEC_METHOD_MAPS = [
        1 => '我司入仓',
        2 => '客户自理',
    ];

    //应付出账状态 1待出账 2部分出账 3已出账
    const PAY_BILL_STATUS = [
        1 => '待出账',
        2 => '部分出账',
        3 => '已出账',
    ];

    //付款状态 1未付款 2部分付款 3已付款
    const PAY_STATUS = [
        1 => '未付款',
        2 => '部分付款',
        3 => '已付款',
    ];

    //香港交货方式
    const HK_LAND_MAPS = [
        0 => '无',
        1 => '其他',
        2 => '国泰',
        3 => '万烽',
        4 => 'AAT',
        5 => '亿高',
    ];

    //香港打单交单
    const HK_PROVIDE_MAPS = [
        0 => '无',
        1 => '其他',
        2 => '速速递',
        3 => '阿捷',
        4 => '日通',
    ];

    //创建人
    public function create_admin(): BelongsTo {
        return $this->belongsTo(AdminModel::class, 'created_admin_id');
    }

    //订单
    public function orders(): HasMany {
        return $this->hasMany(OrderModel::class, 'take_order_id');
    }

    //航班运力
    public function flight(): BelongsTo {
        return $this->belongsTo(FlightModel::class, 'flight_id');
    }

    //客户
    public function customer(): BelongsTo {
        return $this->belongsTo(CustomerModel::class, 'customer_id');
    }

    //万峰小件处理
    public function wf(): HasMany {
        return $this->hasMany(TakeOrderWfModel::class, 'take_order_id');
    }

    //应收
    public function rec(): HasMany {
        return $this->hasMany(TakeOrderRecModel::class, 'take_order_id');
    }

    //清关代理
    public function clr_agent(): BelongsTo {
        return $this->belongsTo(AgentModel::class, 'clr_agent_id');
    }

    //报关代理
    public function dec_agent(): BelongsTo {
        return $this->belongsTo(AgentModel::class, 'dec_agent_id');
    }

    //大客户大包
    public function notify(): HasMany {
        return $this->hasMany(\App\Models\lazada\BigBagModel::class, 'take_order_id');
    }

    public function recs(): HasMany {
        return $this->hasMany(TakeOrderRecModel::class, 'take_order_id');
    }

    public function fill_admin(): BelongsTo {
        return $this->belongsTo(AdminModel::class, 'fill_admin_id');
    }

    public function set_weight_admin(): BelongsTo {
        return $this->belongsTo(AdminModel::class, 'set_weight_admin_id');
    }

    //系统节点
    public function system_node(): BelongsTo {
        return $this->belongsTo(NodeModel::class, 'system_node_id');
    }

    //平台节点
    public function platform_node(): BelongsTo {
        return $this->belongsTo(NodeModel::class, 'platform_node_id');
    }

    //特殊货物
    public function special(): HasMany {
        return $this->hasMany(TakeOrderSpecialModel::class, 'take_order_id');
    }

}
